require(['Jquery','Hui','HuiAdmin','Layer','Icheck','Datepicker','DatepickerCN','Ajaxfileupload'], function(jquery,hui,huiadmin,layer,iCheck){

	// 添加
	article_add = function(){
		$('.add-btn').click(function() {
			var self = $(this),
			url = self.attr("data-url");

			layer.open({
			  type: 2,
			  title: '添加资讯',
			  shade: 0.5,
			  scrollbar : false,
			  area : ['100%','100%'],
			  content: url,
			  cancel: function(){ 
			      //右上角关闭回调
			      pageReload(500);
			    }
			}); 

		});
	}

	//编辑排序值
	edit_sort_num = function(){
		$('.sort-btn').click(function(){
			var self = $(this);
			var sort_num = self.val();
			var id = self.attr('data-id');

			layer.open({
			  type: 1,
			  title: '编辑排序值',
			  shade: 0.5,
			  scrollbar : false,
			  area : ['14%','20%'],
			  'btn': ["确定","取消"],
			  content: $("#sort_num_page").html(),
			  success: function(layero, index){ 
			  		$(layero).find(".sort_num").val(sort_num);
			  },
			  yes : function (index, frame) {
			  	var sort = $(frame).find('.sort_num').val();
			  	var url = $(frame).find('.sort_num').attr('data-url');
			  	// alert(url);alert(id);

			  	if (!sort) {
			  		layer.alert('请输入排序值',{icon: 2});
			  		return false;
			  	}

			  	if (isNaN(sort)) {
			  		layer.alert('排序值必须为数字',{icon: 2});
			  		return false;
			  	}

			  	if ((sort < 1) || (sort > 50)){
			  		layer.alert('必须输入1到50之间数字',{icon: 2});
			  		return false;
			  	}

			  	var params = {id: id,sort_num:sort};
			  	$.post(url,params,function(json) {
			  		if (json.code) {
						layer.alert(json.msg,{icon: 1});
						pageReload(1000);
					} else {
						layer.alert(json.msg,{icon: 5});
					}
			  	},"json");
			  },
			  cancel: function(){ 
			      //右上角关闭回调
			      pageReload(500);
			    }
			}); 
		})

	}
	
	// 改变审核状态
	edit_examine = function(){
		$('.examine-btn').click(function(){
			var self = $(this),
				is_examine = self.parent().attr('data-examine');
				id         = self.parent().attr('data-id');
			var url = exit_examine;

			if (is_examine == 1) {
				_is_examine = 0;
			} else {
				_is_examine = 1;
			}

			layer.msg('确定要改变审核状态吗？', {
				time: 0 ,//不自动关闭,
				btn: ['确定', '关闭'],
				yes: function(index){
				    layer.close(index);
						var params = {id: id,is_examine: _is_examine};
						$.post(url, params, function(json){
							if (json.code) {
								layer.alert(json.msg,{icon: 1});
								pageReload(1000);
							} else {
								layer.alert(json.msg,{icon: 2});
							}
						}, 'json');
				}
			});

		});
	}

	// 改变发布状态
	edit_on = function(){
		$('.on-btn').click(function(){
			var self = $(this),
				is_on = self.parent().attr('data-on');
				id         = self.parent().attr('data-id');
			var url = exit_on;

			if (is_on == 1) {
				_is_on = 0;
			} else {
				_is_on = 1;
			}

			layer.msg('确定要改变发布状态吗？', {
				time: 0 ,//不自动关闭,
				btn: ['确定', '关闭'],
				yes: function(index){
				    layer.close(index);
						var params = {id: id,is_on: _is_on};
						$.post(url, params, function(json){
							if (json.code) {
								layer.alert(json.msg,{icon: 1});
								pageReload(1000);
							} else {
								layer.alert(json.msg,{icon: 2});
							}
						}, 'json');
				}
			});

		});
	}

	// 选择图片
	selectImg = function() {
		$('.img-thumbnail').click(function(event) {
			$(this).next(".uploda-image").click();
		});
	};

	// 上传图片
	uploadImg = function() {
		$('.image-td').on("change",".uploda-image",function(){
			var uploda = $(this),
			    url = uploda.attr('data-url'),
			    fileElementId = uploda.attr('id'),
				id  = uploda.attr('data-id'),
				img = uploda.prev('.img-thumbnail');
			var path = 'article';

				// alert(url);
			$.ajaxFileUpload({
				url : url,
				secureuri:false,
				fileElementId : fileElementId,
				dataType: 'json',
				data:{id: id,path: path},
				success: function (json) {
					if (json.code) {
						layer.alert(json.msg,{icon: 1});
						img.prop('src', json.url);
						// pageReload(1000);
					} else {
						layer.alert(json.msg,{icon: 2});
					}
				},
				error: function (data, status, e) {
					alert(e);
				}
			});
		})
	};

	// 编辑
	article_edit = function(){
		$('.edit-btn').click(function() {
			var self = $(this),
			url = self.attr("data-url");

			layer.open({
			  type: 2,
			  title: '编辑资讯',
			  scrollbar : false,
			  area : ['100%','100%'],
			  content: url,
			  cancel: function(){ 
			      //右上角关闭回调
			      pageReload(500);
			    }
			}); 

		});
	}

	// 资讯详情
	admin_details = function(){
		$('.details-btn').click(function() {
			var self = $(this),
			url = self.attr("data-url");

			layer.open({
			  type: 2,
			  title: '资讯详情',
			  shade: 0.5,
			  scrollbar : false,
			  area : ['80%','80%'],
			  content: url,
			  cancel: function(){ 
			      //右上角关闭回调
			      pageReload(500);
			    }
			}); 

		});
	}

	// 删除
	admin_del = function() {
		$('.delete-btn').click(function(){
			var self = $(this),
				id   = self.attr('data-id'),
				url  = self.attr('data-url');
			layer.msg('确定要删除吗？', {
				time: 0 ,//不自动关闭,
				btn: ['确定', '关闭'],
				yes: function(index){
				    layer.close(index);
						var params = {id: id};
						$.post(url, params, function(json){
							if (json.code) {
								layer.alert(json.msg,{icon: 1});
								pageReload(1000);
							} else {
								layer.alert(json.msg,{icon: 2});
							}
						}, 'json');
				}
			});
		});
	};

	// 批量处理
	batch_proc = function(){
		$('.batch-btn').click(function(){
			var self = $(this),
				url  = self.attr('data-url'),
				type  = self.attr('data-type'),
				title  = self.attr('data-title');
			var ids = get_checkbox_group_value('article_id[]');

			if (ids == false) {
				layer.alert('请选择要批量'+ title +'的资讯',{icon: 2});
				return false;
			}
// alert(ids);
			layer.alert('确定要批量'+ title +'选中资讯吗？', {
				time: 0 ,//不自动关闭,
				btn: ['确定', '关闭'],
				yes: function(index){
				    layer.close(index);
						var data = {ids: ids, type:type};
						$.post(url, data, function(json){
							if (json.code) {
								layer.alert(json.msg,{icon: 1});
								pageReload(1000);
							} else {
								layer.alert(json.msg,{icon: 5});
							}
						}, 'json');
				}
			});
		})
	}

	// 重置
	reset = function() {
		$('.btn-reset').click(function(){
			var form = $('#search-form');
			$('.arta-cid option').each(function(){
				if ($(this).prop("selected")) {
					$(this).removeProp("selected");
				}
			});
			form.find('input').each(function(){
				$(this).iCheck('uncheck');
			});

			form.find('[name="start"]').val('');
			form.find('[name="stop"]').val('');
			form.find('[name="title"]').val('');
		});
	};

	// 单选框复选框样式事件
	iCheckStyle('select_all','article_id[]');

	//日期选择
	datepicker('#search-form','.datepicker-style');

	// 添加
	article_add();

	//编辑排序值
	edit_sort_num();

	// 改变审核状态
	edit_examine();

	// 改变发布状态
	edit_on();

	// 选择图片
	selectImg();

	// 上传图片
	uploadImg();

	// 编辑
	article_edit();

	// 删除
	admin_del();

	// 资讯详情
	admin_details();

	// 批量处理
	batch_proc();

	// 重置
	reset();

});